{% extends 'base/base.html' %}

{% block title %}项目成员 - {{ project.name }} - 多用户报警系统{% endblock %}

{% block content %}
<div class="mb-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1>
            <i class="fa fa-users" aria-hidden="true"></i> 项目成员 - {{ project.name }}
        </h1>
        {% if user_role == 'admin' %}
            <a href="{% url 'projects:project_add_member' project.id %}" class="btn btn-primary">
                <i class="fa fa-user-plus" aria-hidden="true"></i> 添加成员
            </a>
        {% endif %}
    </div>
    
    <div class="card">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>用户名</th>
                            <th>角色</th>
                            <th>加入时间</th>
                            {% if user_role == 'admin' %}
                                <th>操作</th>
                            {% endif %}
                        </tr>
                    </thead>
                    <tbody>
                        {% for member in members %}
                            <tr>
                                <td>{{ forloop.counter }}</td>
                                <td>{{ member.user.username }}</td>
                                <td><span class="role-badge role-{{ member.role }}">{{ member.get_role_display }}</span></td>
                                <td>{{ member.joined_at|date:"Y年m月d日 H:i:s" }}</td>
                                {% if user_role == 'admin' and member.user != request.user %}
                                    <td>
                                        <div class="d-flex gap-1">
                                            <a href="{% url 'projects:project_update_member' project.id member.id %}" class="btn btn-warning btn-sm" title="修改角色">
                                                <i class="fa fa-pencil" aria-hidden="true"></i>
                                            </a>
                                            <a href="{% url 'projects:project_remove_member' project.id member.id %}" class="btn btn-danger btn-sm" title="移除成员" onclick="return confirm('确定要移除该成员吗？');">
                                                <i class="fa fa-trash" aria-hidden="true"></i>
                                            </a>
                                        </div>
                                    </td>
                                {% elif user_role == 'admin' %}
                                    <td>
                                        <span class="text-muted small">您自己</span>
                                    </td>
                                {% endif %}
                            </tr>
                        {% empty %}
                            <tr>
                                <td colspan="{% if user_role == 'admin' %}5{% else %}4{% endif %}" class="text-center text-muted">
                                    <i class="fa fa-users" aria-hidden="true"></i> 暂无项目成员
                                </td>
                            </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
        <div class="card-footer bg-white">
            <div class="text-muted">
                <small><i class="fa fa-info-circle" aria-hidden="true"></i> 角色说明：管理员可以管理项目和成员，编辑可以编辑项目内容，查看者只能查看项目内容。</small>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block javascript %}
<script>
    // 添加页面加载动画效果
    document.addEventListener('DOMContentLoaded', function() {
        // 为操作按钮添加悬停效果
        const actionButtons = document.querySelectorAll('.btn-sm');
        actionButtons.forEach(button => {
            button.addEventListener('mouseenter', function() {
                this.style.transform = 'scale(1.1)';
            });
            button.addEventListener('mouseleave', function() {
                this.style.transform = 'scale(1)';
            });
        });
        
        // 为删除按钮添加确认提示
        const deleteButtons = document.querySelectorAll('.btn-danger');
        deleteButtons.forEach(button => {
            button.addEventListener('click', function(e) {
                if (!confirm('确定要移除该成员吗？')) {
                    e.preventDefault();
                }
            });
        });
    });
</script>
{% endblock %}